package ${basePackage}.service;

import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import ${basePackage}.domain.${methodName};
import ${basePackage}.dto.${methodName}DTO;
import ${basePackage}.error.CustomParameterizedException;
import ${basePackage}.mapper.${methodName}Mapper;
import ${basePackage}.repository.${methodName}Repository;
import ${basePackage}.vo.${methodName}VO;
import ${basePackage}.vo.PagerVO;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author: sovy
 * @date: ${createTime}
 * @description:
 */
@Service
@AllArgsConstructor
public class ${methodName}Service {

    private ${methodName}Repository ${defineName}Repository;

    private ${methodName}Mapper ${defineName}Mapper;

    /**
     * 条件查询
     *
     * @return
     */
    public PagerVO<${methodName}VO> list(Integer page, Integer pageSize) {
        Page<${methodName}> ${defineName}Page = ${defineName}Repository.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
            return null;
        }, PageRequest.of(page <= 0 ? 0 : (page - 1), pageSize));
        return PagerVO.of(page, pageSize, ${defineName}Page.getTotalElements(), ${defineName}Mapper.get${methodName}VO(${defineName}Page.getContent()));
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public ${methodName} get(Integer id) {
        Optional<${methodName}> ${defineName}Optional = ${defineName}Repository.findById(id);
        if (${defineName}Optional.isPresent()) {
            return ${defineName}Optional.get();
        }
        throw new CustomParameterizedException("不存在");
    }

    /**
     * 获取返回详情
     *
     * @param id
     * @return
     */
    public ${methodName}VO get${methodName}VO(Integer id) {
        ${methodName} ${defineName} = get(id);
        return ${defineName}Mapper.get${methodName}VO(${defineName});
    }

    /**
     * 增加|编辑
     */
    public void save(${methodName}DTO ${defineName}DTO) {
        ${methodName} ${defineName};
        if (${defineName}DTO.getId() != null && ${defineName}DTO.getId() > 0) {
            ${defineName} = get(${defineName}DTO.getId());
        } else {
            ${defineName} = ${methodName}
                    .builder()
                    .build();
        }
        ${defineName}Mapper.copyTo${methodName}(${defineName}, ${defineName}DTO);
        ${defineName}Repository.save(${defineName});
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(Integer id) {
        ${methodName} ${defineName} = get(id);
        ${defineName}Repository.delete(${defineName});
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void delete(List<Integer> ids) {
        ids.forEach(id -> {
            delete(id);
        });
    }
}